/**
 * Created by acer on 2015/7/22.
 */
var updateOrderUrl='/order/updateOrder',
    totalValidData;

$(document).ready(function(){
    $("#form").steps({
        bodyTag: "fieldset",
        labels: {
            current: "当前流程 :",
            pagination: "Pagination",
            finish: "完成",
            next: "下一步",
            previous: "上一步",
            loading: "加载中 ..."
        },
        enableCancelButton: 0,
        enableKeyNavigation: 0,
        enableFinishButton: 0,
        onStepChanging: function (event, currentIndex, newIndex)
        {
            var validData
            if(currentIndex==0){
                var checkInput=new Array()
                $('.i-checks').each(function(e){
                    if($(this).find('input').is(':checked')==true){
                        var thisInputID=$(this).find('input').attr('id')
                        checkInput.push(thisInputID)
                    }
                })
                if(checkInput.length>1){
                    alert('不可选择多个订单')
                }
                else if(checkInput.length==1){
                    for(var i in totalValidData){
                        if(checkInput[0]==totalValidData[i]._id){
                             forthvalidData=validData=totalValidData[i]
                        }
                    }
                    $('.searchGroupSecond').each(function(e){
                        $('.search-hasResult').show()
                        var thisInputID=$(this).attr('id').replace('NeverSecond','')
                        for(var i in validData){
                            if(thisInputID==i){
                                $(this).val(validData[i])
                            }
                        }
                    })
                    $('.searchGroupThird').each(function(e){
                        var thisInputID=$(this).attr('id').replace('NeverThird','')
                        for(var i in validData){
                            if(thisInputID==i){
                                $(this).val(validData[i])
                            }
                        }
                    })
                    var snNoTemplate='<div class="theme-content-row"><div class="theme-content-row-name">SN号码:</div><div class="theme-content-row-detail"><input type="text" class="form-control snNoArray"/></div></div>'
                    var snNoTemplateEnsure='<div class="theme-content-row"><div class="theme-content-row-name">SN号码:</div><div class="theme-content-row-detail"><input type="text" class="form-control snNoArrayEnsure justShow" disabled="disabled"/></div></div>'
                    $('#snNoTemplate').html('')
                    $('#snNoTemplateEnsure').html('')
                    for(var j=0;j<validData.FDEVICEQUANTITY;j++){
                        $('#snNoTemplate').append(snNoTemplate)
                        $('#snNoTemplateEnsure').append(snNoTemplateEnsure)
                    }
                    var form = $(this);

                    // Disable validation on fields that are disabled or hidden.
                    form.validate().settings.ignore = ":disabled,:hidden";

                    // Start validation; Prevent going forward if false
                    return form.valid();
                }
                else{
                    alert('请选择订单')
                }

            }
            else if(currentIndex==1&&newIndex>currentIndex){
                 deviceListArray=new Array();
                $('.snNoArray').each(function(){
                    var temporaryObject={}
                    temporaryObject['FSN']=$(this).val()
                    deviceListArray.push(temporaryObject)
                })
                for(var n=0;n<deviceListArray.length;n++){
                    $('.snNoArrayEnsure:eq('+n+')').val(deviceListArray[n].FSN)
                }
                var form = $(this);

                // Disable validation on fields that are disabled or hidden.
                form.validate().settings.ignore = ":disabled,:hidden";

                // Start validation; Prevent going forward if false
                return form.valid();
            }
            else if(currentIndex==2&&newIndex>currentIndex){
                 for(var x in forthvalidData){
                     $('#'+x+'Forth').val(forthvalidData[x])
                 }
                $('#snNoArrayLast').html('')
                var snNoTemplateLast='<div class="theme-content-row"><div class="theme-content-row-name">SN号码:</div><div class="theme-content-row-detail"><input type="text" class="form-control snNoArrayLast justShow" disabled="disabled"/></div></div>'
                for(var z in deviceListArray){
                    $('#snNoArrayLast').append(snNoTemplateLast)
                    $('.snNoArrayLast:eq('+z+')').val(deviceListArray[z].FSN)
                }
                var form = $(this);

                // Disable validation on fields that are disabled or hidden.
                form.validate().settings.ignore = ":disabled,:hidden";

                // Start validation; Prevent going forward if false
                return form.valid();
            }
        },
        onStepChanged: function (event, currentIndex, priorIndex)
        {

        },
        onFinishing: function (event, currentIndex)
        {
            var form = $(this);

            // Disable validation on fields that are disabled.
            // At this point it's recommended to do an overall check (mean ignoring only disabled fields)
            form.validate().settings.ignore = ":disabled";

            // Start validation; Prevent form submission if false
            return form.valid();
        },
        onFinished: function (event, currentIndex)
        {
            var form = $(this);

            // Submit form input
            form.submit();
        }
    }).validate({
        errorPlacement: function (error, element)
        {
            element.before(error);
        },
        rules: {
            confirm: {
                equalTo: "#password"
            }
        }
    });
    //try{
    //    $('.timeAccurate').datepicker({
    //        minView:'0',
    //        format: 'yyyy-mm-dd hh:ii:ss',
    //        pickTime: false,
    //        autoclose:true,
    //        language:"cn",
    //        todayBtn:  1
    //    });
    //}
    //catch(err){
    //    console.log('无需时间控件')
    //}
})


var Mymodel={}
Mymodel.GetMessageList=ko.observableArray([]);
Mymodel.GetPaging=ko.observableArray([]);

searchIt=function(){
    var param={}
    $('.searchGroupFirst').each(function(e){
        if(isNull($(this).val())==false){
            param[$(this).attr('id')]=$(this).val()
        }
    })

    var params = {
        url:appSocket+commmonUrlArray.findOrderByConditionsUrl+'?pageSize=1000000&&startAt=0&&'+decodeURIComponent($.param(param)).replace(/'/g,''),
        data:{},
        method:'get'
    }
    ajaxRequest(params,function(data){
        var validData=data.recordset
        var aimAtIt=$('#appendSF')
        aimAtIt.html('')
        if(validData!=''){
            for(var i in validData){
                //if(validData[i].FORDERSTATUS==undefined){
                //    validData[i].FORDERSTATUS=''
                //}
                try{validData[i].FLEASEBEGINTIME=new Date(validData[i].FLEASEBEGINTIME).Format('yyyy-mm-dd')}
                catch(err){

                }
                try{validData[i].FLEASEENDTIME=new Date(validData[i].FLEASEENDTIME).Format('yyyy-mm-dd')}
                catch(err){

                }
                try{validData[i].FTAKEAWAYTIME=new Date(validData[i].FTAKEAWAYTIME).Format('yyyy-mm-dd hh:ii')}
                catch(err){

                }
                var template='<tr><td class="table-checkbox"><label class="i-checks"><input type="checkbox" id="'+validData[i]._id+'"></label></td><td class="center">'+validData[i].FCHANNELSORDERNO+'</td><td class="center">'+validData[i].FEXPRESSRECIPIENT+'</td><td class="center">'+validData[i].FEXPRESSRECIPIENTTEL+'</td></tr>'
                aimAtIt.append(template)
            }
            totalValidData=validData.slice()
        }
    })
}

submmitIt=function(){
    var param={
        url:appSocket+commmonUrlArray.exWareHouseUrl,
        data:{
            "FDEVICELIST":deviceListArray,
            _id:forthvalidData._id
        },
        method:'post'
    }
    ajaxRequest(param,function(data){
     if(data.status==200){
         alert('出库成功')
         window.location.reload()
     }
    })
}

ko.applyBindings(Mymodel);